﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace BAR_POS
{
    public partial class OrderEntryScreen : DevExpress.XtraEditors.XtraUserControl
    {
        public static OrderEntryScreen _Main;
        private static bool UpdateTicket = false;
        public static double CalculatedSubTotal = 0.00;

        public OrderEntryScreen()
        {
            InitializeComponent();
            _Main = this;
            listView1.BackColor = this.BackColor;
            listView1.ForeColor = this.ForeColor;

            lblServerName.Text = StaticProperties.ServerName;
            lblTableName.Text = StaticProperties.TableName;

            Random random = new Random();
            int randomNumber = random.Next(0, 100000000);

            StaticProperties.ReceiptNo = randomNumber; //  Convert.ToInt32(StaticProperties.ReceiptNoGenerator());

            lblReceiptNo.Text = StaticProperties.ReceiptNo.ToString();

            UpdateTicket = false;

            CalculatedSubTotal = 0.00;
            lblSubTotal.Text = CalculatedSubTotal.ToString("C");
        }

        public OrderEntryScreen(int ReceiptNo)
        {
            InitializeComponent();
            _Main = this;
            listView1.BackColor = this.BackColor;
            listView1.ForeColor = this.ForeColor;
            UpdateTicket = true;
            CalculatedSubTotal = 0.00;

            StaticProperties.ReceiptNo = ReceiptNo;

            foreach (DataRow Receipt in DataTableClass.Receipt.Rows)
            {
                if(Receipt["ReceiptNo"].ToString().Trim() == StaticProperties.ReceiptNo.ToString())
                {
                    lblReceiptNo.Text = Receipt["ReceiptNo"].ToString().Trim();
                    lblTableName.Text = Receipt["ReceiptTableName"].ToString().Trim();

                    foreach (DataRow Employee in DataTableClass.EmployeeLogin.Rows)
                    {
                        if (Employee["EmployeeID"].ToString().Trim() == Receipt["ReceiptServerID"].ToString().Trim())
                        {
                            lblServerName.Text = Employee["EmployeeFullName"].ToString().Trim();
                        }
                    }
                }
            }

            foreach (DataRow item in DataTableClass.ReceiptData.Rows)
            {
                if (item["ReceiptNo"].ToString().Trim() == ReceiptNo.ToString())
                {


                    string subtotal = (item["ReceiptItemTotal"]).ToString();
                    double subtotal1 = Convert.ToDouble(subtotal);


                    ListViewItem lvi = new ListViewItem((item["ReceiptQty"]).ToString().Trim());
                    lvi.SubItems.Add((item["ReceiptDescription"]).ToString().Trim());
                    lvi.SubItems.Add(subtotal1.ToString("C"));

                    if (Convert.ToBoolean(item["SentToPrinter"]))
                    {
                        lvi.ForeColor = Color.Green;
                    }

                    listView1.Items.Add(lvi);
                    CalculatedSubTotal += subtotal1;
                }
            }

            lblSubTotal.Text = CalculatedSubTotal.ToString("C");
        }

        private void OrderEntryScreen_Load(object sender, EventArgs e)
        {
            panelControl1.Controls.Add(new MenuItemsButtons("DRINK"));
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            MainProgram._Main.panelControl1.Controls.Clear();
            MainProgram._Main.panelControl1.Controls.Add(new HomeScreen());
        }

        private void btnSendTicket_Click(object sender, EventArgs e)
        {
            if (!UpdateTicket)
            {
                try
                {
                    DataRow newReceipt = DataTableClass.Receipt.NewRow();//dataSet1.Tables["Customers"].NewRow();

                    //newCustomersRow["EntryNo"] = lblReceiptNo.Text;
                    newReceipt["ReceiptNo"] = lblReceiptNo.Text;
                    newReceipt["ReceiptDate"] = DateTime.Now.ToShortDateString();
                    newReceipt["ReceiptTableName"] = lblTableName.Text;
                    newReceipt["ReceiptServerID"] = StaticProperties.ServerID.ToString();
                    newReceipt["ReceiptSubTotal"] = decimal.Parse(lblSubTotal.Text, System.Globalization.NumberStyles.Currency);

                    string SubtotalString = lblSubTotal.Text;
                    string[] subtotalstring = SubtotalString.Split('$');
                    double subtotal = Convert.ToDouble(subtotalstring[1]);
                    double Tax = subtotal * 0.0825;

                    newReceipt["ReceiptTaxTotal"] = Tax.ToString();
                    newReceipt["ReceiptAutoGrat"] = "0.00";
                    newReceipt["ReceiptGrat"] = "0.00";
                    newReceipt["ReceiptDiscountTotal"] = "0.00";
                    newReceipt["ReceiptGrandTotal"] = "0.00";
                    newReceipt["ReceiptStatus"] = false;


                    DataTableClass.Receipt.Rows.Add(newReceipt);

                    int r = 1;
                    r = Convert.ToInt32(lblReceiptNo.Text);
                    foreach (ListViewItem item in listView1.Items)
                    {
                        DataRow newReceiptData = DataTableClass.ReceiptData.NewRow();//dataSet1.Tables["Customers"].NewRow();

                        //newCustomersRow["EntryNo"] = r.ToString();
                        newReceiptData["ReceiptNo"] = lblReceiptNo.Text;
                        newReceiptData["ReceiptQty"] = item.SubItems[0].Text;
                        newReceiptData["ReceiptDescription"] = item.SubItems[1].Text;

                        string total = item.SubItems[2].Text.Replace("$", "");
                        newReceiptData["ReceiptItemTotal"] = total;
                        newReceiptData["SentToPrinter"] = true;

                        DataTableClass.ReceiptData.Rows.Add(newReceiptData);
                    }
                }
                catch(Exception E)
                {
                    MessageBox.Show(E.Message);
                }
            }
            else if (UpdateTicket)
            {
                foreach (ListViewItem item in listView1.Items)
                {
                    if (item.ForeColor != Color.Green)
                    {
                        DataRow newReceiptData = DataTableClass.ReceiptData.NewRow();

                        //newCustomersRow["EntryNo"] = r.ToString();
                        newReceiptData["ReceiptNo"] = lblReceiptNo.Text;
                        newReceiptData["ReceiptQty"] = item.SubItems[0].Text;
                        newReceiptData["ReceiptDescription"] = item.SubItems[1].Text;

                        string total = item.SubItems[2].Text.Replace("$", "");
                        newReceiptData["ReceiptItemTotal"] = total;
                        newReceiptData["SentToPrinter"] = true;

                        DataTableClass.ReceiptData.Rows.Add(newReceiptData);
                    }
                }

                foreach (DataRow receipt in DataTableClass.Receipt.Rows)
                {
                    if (receipt["ReceiptNo"].ToString().Trim() == lblReceiptNo.Text)
                    {
                        string subtotal = lblSubTotal.Text;
                        string[] subtotal1 = subtotal.Split('$');
                        receipt["ReceiptSubTotal"] = subtotal1[1];

                        double amount = Convert.ToDouble(subtotal1[1]);
                        double tax = amount * 0.0825;
                        receipt["ReceiptTaxTotal"] = tax.ToString();

                    }
                }

                UpdateTicket = false;
            }

            MainProgram._Main.panelControl1.Controls.Clear();
            MainProgram._Main.panelControl1.Controls.Add(new HomeScreen());
        }
    }
}
